<script>
	import Vue from 'vue'
	export default {
		globalData: {
			theme: {
				themeColor: '', //主题颜色
				matchColor: '', //配色颜色
				bottomColor: '', //底色颜色
				tabbarItems: [] // 底部导航
			},
			isLoadding: false, //防止多个接口token过期 静默登录 
		},
		onLaunch: function() {
			// 小程序版本更新
			// #ifdef MP
			this.updateManager();
			// #endif
			//获取商城主题
			this.getMobileTheme();

			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
		},
		methods: {
			// 全局方法 
			initPage() {
				let that = this;
				return new Promise((resolve, reject) => {
					if (!uni.getStorageSync('hxtoken')) {
						//无token，进行登录
						// #ifdef MP-WEIXIN
						that.loginWxMa().then(res => {
							resolve("success");
						});
						// #endif
						// #ifdef MP-ALIPAY
						that.loginAliMa().then(res => {
							resolve("success");
						});
						// #endif
						// #ifndef MP-WEIXIN || MP-ALIPAY
						that.userAppAuth().then(res => {
							resolve("success");
						});
						// #endif
					} else {
						// #ifdef MP-WEIXIN
						wx.checkSession({
							success() {
								//session_key 未过期
								console.log("session_key未过期")
							},
							fail() {
								console.log("session_key已过期")
								// session_key 已经失效，需要重新登录
								that.loginWxMa().then(res => {
									resolve("success");
								});
							}
						})
						// #endif
						// // #ifdef MP-ALIPAY
						// that.aliPayCheckToken().then(res => {
						// 	resolve("success");
						// });
						// // #endif
						resolve("success");
					}
				});
			},
			userAppAuth() {
				return new Promise((resolve, reject) => {
					let that = this;
					uni.$myRequest.userAppAuth({}).then(res => {
						uni.hideLoading();
						let userInfo = res.data;
						uni.setStorageSync('hxtoken', userInfo.hxToken);
						uni.setStorageSync('userInfo', userInfo);
						console.log("静默登录成功" + JSON.stringify(userInfo));
						that.globalData.isLoadding = false
						resolve();
					});
				});
			},
			/**
			 * 获取主题
			 */
			getMobileTheme() {
				return new Promise((resolve, reject) => {
					let that = this;
					uni.$myRequest.mobileTheme().then(res => {
						let data = res.data;
						that.globalData.theme.themeColor = data.themeColor
						that.globalData.theme.matchColor = data.matchColor
						that.globalData.theme.bottomColor = data.bottomColor
						// 初始化 底部导航
						this.tabbarItems = [{
								index: 0,
								text: '首页',
								iconPath: 'static/image/home.png',
								selectedIconPath: 'static/image/tabbar/tabbar-home-hx-red.png',
								visible: true
							},
							{
								index: 1,
								text: '分类',
								iconPath: 'static/image/category.png',
								selectedIconPath: 'static/image/tabbar/tabbar-category-hx-red.png',
								visible: true
							},
							{
								index: 2,
								text: '购物车',
								iconPath: 'static/image/shopping-cart.png',
								selectedIconPath: 'static/image/tabbar/tabbar-shoppingcart-hx-red.png',
								visible: true
							},
							{
								index: 3,
								text: '我的',
								iconPath: 'static/image/user-info.png',
								selectedIconPath: 'static/image/tabbar/tabbar-user-hx-red.png',
								visible: true
							},
						]
						if (data.tabbarItem) {
							let tabbars = []
							data.tabbarItem.info.forEach(item => {
								tabbars.push({
									index: item.index,
									text: item.text,
									iconPath: 'static/image/' + item.iconPath,
									selectedIconPath: 'static/image/tabbar/' + item
										.selectedIconPath,
									visible: item.visible
								})
							})
							this.tabbarItems = tabbars
						}

						// 设置tabbar样式
						uni.setTabBarStyle({
							color: data.tabbarColor ? data.tabbarColor : '#666666',
							selectedColor: data.tabbarSelectedColor ? data.tabbarSelectedColor :
								'#f9002c',
							backgroundColor: data.tabbarBgColor ? data.tabbarBgColor : '#ffffff',
							borderStyle: data.tabbarBorderStyle ? data.tabbarBorderStyle : 'white'
						})

						this.tabbarItems.forEach(item => {
							uni.setTabBarItem({
								index: item.index,
								text: item.text,
								iconPath: item.iconPath,
								selectedIconPath: item.selectedIconPath,
								visible: item.visible
							});
						})

						resolve("success");
					});
				});
			},
			//微信小程序登录
			// #ifdef MP-WEIXIN
			loginWxMa() {
				return new Promise((resolve, reject) => {
					let that = this;
					uni.login({
						success: function(res) {
							console.log("res++++++++++" + JSON.stringify(res));
							if (res.code) {
								let wxUserDTO = {
									wxCode: res.code
								};
								uni.$myRequest.userAppAuth({
									wxUserDTO: wxUserDTO
								}).then(res => {
									uni.hideLoading();
									let userInfo = res.data;
									uni.setStorageSync('hxtoken', userInfo.hxToken);
									uni.setStorageSync('userInfo', userInfo);
									console.log("小程序登录成功" + JSON.stringify(userInfo));
									that.globalData.isLoadding = false
									resolve("success");
								});
							}
						}
					});
				});
			},
			// #endif
			//支付宝小程序静默登录
			// #ifdef MP-ALIPAY
			loginAliMa() {
				return new Promise((resolve, reject) => {
					let that = this;
					uni.login({
						provider: 'ali',
						scopes: 'auth_base',
						success: function(res) {
							console.log("res++++++++++" + JSON.stringify(res));
							if (res.code) {
								let aliUserDTO = {
									aliCode: res.code
								};
								uni.$myRequest.userAppAuth({
									aliUserDTO: aliUserDTO
								}).then(res => {
									uni.hideLoading();
									let userInfo = res.data;
									uni.setStorageSync('hxtoken', userInfo.hxToken);
									uni.setStorageSync('userInfo', userInfo);
									console.log("支付宝小程序登录成功" + JSON.stringify(userInfo));
									that.globalData.isLoadding = false
									resolve("success");
								});
							}
						}
					});
				});
			},
			// 支付宝小程序检查token是否过期
			// aliPayCheckToken(){
			// 	uni.$myRequest.alipayCheckToken().then(res => {
			// 		let userInfo = res.data;
			// 		if(userInfo){
			// 			uni.setStorageSync('hxtoken', userInfo.hxToken);
			// 			uni.setStorageSync('userInfo', userInfo);
			// 			that.globalData.isLoadding = false
			// 		}
			// 		resolve("success");
			// 	});
			// },
			// #endif
			// #ifdef MP
			// 小程序版本更新
			updateManager() {
				const updateManager = uni.getUpdateManager();
				updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});

				});
			}
			// #endif

		}
	}
</script>
<style lang="scss">
	@import "./components/colorui/main.css";
	@import "./components/colorui/icon.css";

	.hx-overflow-1 {
		display: -webkit-box; //谷歌
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1; //显示几行
		overflow: hidden;
	}

	.hx-overflow-2 {
		display: -webkit-box; //谷歌
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; //显示几行
		overflow: hidden;
	}

	.bg-hx-green {
		background-color: #13d694;
		color: #fff;
	}

	.bg-hx-li-green {
		background-color: #313331;
		color: #fff;
	}

	.bg-hx-pink {
		background-color: #fe496e;
		color: #fff;
	}

	.bg-hx-li-pink {
		background-color: #ffe1e8;
		color: #fe496e;
	}

	.bg-hx-golden {
		background-color: #bb9c62;
		color: #fff;
	}

	.bg-hx-li-golden {
		background-color: #f1ebdd;
		color: #bb9c62;
	}

	.bg-hx-red {
		background-color: #f9002c;
		color: #fff;
	}

	.bg-hx-li-red {
		background-color: #ff8025;
		color: #fff;
	}

	.bg-hx-blue {
		background-color: #1c73f0;
		color: #fff;
	}

	.bg-hx-li-blue {
		background-color: #fe6220;
		color: #fff;
	}

	.text-hx-green {
		color: #13d694;
	}

	.text-hx-li-green {
		color: #313331;
	}

	.text-hx-pink {
		color: #fe496e;
	}

	.text-hx-li-pink {
		color: #ffe1e8;
	}

	.text-hx-golden {
		color: #bb9c62;
	}

	.text-hx-li-golden {
		color: #f1ebdd;
	}

	.text-hx-red {
		color: #f9002c;
	}

	.text-hx-li-red {
		color: #ff8025;
	}

	.text-hx-blue {
		color: #1c73f0;
	}

	.text-hx-li-blue {
		color: #fe6220;
	}
</style>
